/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.find.view;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import com.suozhang.framework.utils.NetUtil;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.widget.ZoomImageView;
import com.yiqi.lottery.entity.bo.CircleBo;

public class ImageActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    private List<CircleBo.PostFilesBean> mArrayBos = null;

    private List<ImageView> mImageViews = new ArrayList<>();

    private int mPosition;


    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_image;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        super.initView();

    }

    @Override
    protected void initData() {
        mPosition = this.getIntent().getIntExtra("pos", 0);
        mArrayBos = (List<CircleBo.PostFilesBean>) this.getIntent().getSerializableExtra("images");
        if (mArrayBos==null){
            return;
        }

        for (int i = 0; i < mArrayBos.size(); i++) {
            ZoomImageView imageView = new ZoomImageView(this,null);
            AM.image().bind(mArrayBos.get(i).getFile_url(), imageView);
            mImageViews.add(imageView);
        }

        initToolBar(mToolbar, mPosition + 1 + "/" + mImageViews.size(), true, true);
        mViewPager.setAdapter(pagerAdapter);

        mViewPager.setCurrentItem(mPosition);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                initToolBar(mToolbar, position + 1 + "/" + mImageViews.size(), true, true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    PagerAdapter pagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return mImageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            (container).removeView(mImageViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ( container).addView(mImageViews.get(position));
            return mImageViews.get(position);
        }
    };


}